package cn.gzy.service.impl;

import cn.gzy.mapper.BlogMapper;
import cn.gzy.mapper.BlogTypeMapper;
import cn.gzy.mapper.UserMapper;
import cn.gzy.pojo.*;
import cn.gzy.service.BlogService;
import cn.gzy.util.PageUtil;
import cn.gzy.vo.BlogVo;
import cn.gzy.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BlogImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogTypeMapper blogTypeMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageUtil<BlogVo> findRecently(Integer current, Integer size, Map<String, Object> map) {
        Integer count = blogMapper.searchCountByCondition(map);
        PageUtil<BlogVo> page = new PageUtil<>(current, size, count);
        List<BlogVo> blogVoList = blogMapper.searchByCondition(map, (page.getCurrent() - 1) * page.getSize(), page.getSize());
        page.setList(blogVoList);

        blogVoList.forEach(blogVo -> {
            Integer authorId = blogVo.getAuthor(); // 假设getAuthor()返回的是用户ID
            if (authorId != null) { // 检查authorId是否为null
                User author = userMapper.searchById(authorId);
                if (author != null) { // 检查author是否为null
                    blogVo.setAuthorName(author.getNickname());
                } else {
                    // 如果author为null，可以设置一个默认值或者处理逻辑
                    blogVo.setAuthorName("未知作者"); // 例如，设置为"未知作者"
                    // 或者，您也可以选择记录日志或抛出更具体的异常
                    // log.error("无法找到ID为{}的用户", authorId);
                    // throw new RuntimeException("无法找到ID为" + authorId + "的用户");
                }
            } else {
                // 如果authorId本身为null，也可以设置默认值或处理逻辑
                blogVo.setAuthorName("未知作者");
                // 或者记录日志等
            }
        });

        return page;
    }

    @Override
    public List<BlogType> findAllType() {
        return blogTypeMapper.searchAllType();
    }

    @Override
    public Integer addBlog(Blog blog) {
        return blogMapper.addBlog(blog);
    }

    @Override
    public BlogVo showOneBlog(Integer id) {
        BlogVo blogVo = blogMapper.findById(id);
        Blog blog = new Blog();
        blog.setBlogId(blogVo.getBlogId());
        blog.setReaded(blogVo.getReaded()+1); //修改点击量
        blogVo.setReaded(blogVo.getReaded()+1);
        blogMapper.updateById(blog);
        return blogVo;
    }

    @Override
    public List<CommentVo> findCommentsByBlogId(Integer blogId) {
        return blogMapper.findCommentsByBlogId(blogId);
    }

    @Override
    public List<Blog> findRecommend() {
        return blogMapper.searchRecommend();
    }

    @Override
    public Integer newComment(Comment comment) {
        return blogMapper.newComment(comment);
    }

    @Override
    public Integer updateReaded(Blog blog) {
        return blogMapper.updateById(blog);
    }

    @Override
    public Integer liked(Liked liked) {
        blogMapper.liked(liked);
        return blogMapper.countOfLiked(liked.getIBlogId());
    }

    @Override
    public Integer noLike(Liked liked) {
        blogMapper.noLike(liked);
        return blogMapper.countOfLiked(liked.getIBlogId());
    }

    @Override
    public Integer isLiked(Integer blogId, Integer userId) {
        return blogMapper.isLiked(blogId,userId);
    }

    public Integer favorite(Favorite favorite) {
        blogMapper.favorite(favorite);
        return blogMapper.countOfFavorite(favorite.getFBlogId());
    }
    @Override
    public Integer noFavorite(Favorite favorite) {
        blogMapper.noFavorite(favorite);
        return blogMapper.countOfFavorite(favorite.getFBlogId());
    }

    @Override
    public Integer isFavorited(Integer blogId, Integer userId) {
        return blogMapper.isFavorited(blogId,userId);
    }

    @Override
    public Integer setpremium(Integer blogId) {
        return blogMapper.setpremium(blogId);
    }
    @Override
    public Integer unsetpremium(Integer blogId) {
        return blogMapper.unsetpremium(blogId);
    }

    @Override
    public Blog isPremium(Integer blogId) {
        return blogMapper.isPremium(blogId);
    }
}
